/*
 *  Command_Completion.cpp
 *  osnutshell
 *
 *  Created by Zachary  Heylmun on 4/18/10.
 *  Copyright 2010 University of Florida. All rights reserved.
 *
 */

#include "Command_Completion.h"

using std::string;
using std::vector;

namespace nutshell
{
	// stores recent commands for command completion
	vector<string> Command_Completion::recent_commands;
	
	// stores recent command lines for command line history
	vector<string> Command_Completion::recent_command_lines;

	// add a command for later command completion
	void Command_Completion::add_command(string s)
	{
		recent_commands.push_back(s);
	}

	// add a command line for later command line history lookup
	void Command_Completion::add_command_line(string s)
	{
		if (recent_command_lines.empty())
		{
			recent_command_lines.push_back(s);
			recent_command_lines.push_back("");
		}
		else
		{
			recent_command_lines.back() = s;
			recent_command_lines.push_back("");
		}
	}

	// returns a vector of recent command lines for command line history
	const vector<string>& Command_Completion::get_recent_lines()
	{
		return recent_command_lines;
	}

	// returns a vector of recend commands that match the provided command prefix
	vector<string> Command_Completion::complete_command(string s)
	{
		vector<string> matches;
	
		vector<string>::const_reverse_iterator itr = recent_commands.rbegin();
		for (; itr != recent_commands.rend(); itr++)
		{
			if (itr->find(s) == 0)
				matches.push_back(*itr);
		}

		return matches;
	}
	
}
